//
//  XNCareConfirmViewController.m
//  XINNingOA
//
//  Created by samuel on 2021/7/14.
//

#import "XNCareConfirmViewController.h"
#import "XNCareConfirmBillInfoModel.h"
#import "XNCareConfirmApproveRequest.h"
#import "XNCareConfirmBillInfoRequest.h"

@interface XNCareConfirmViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) XNCareConfirmBillInfoModel *infoModel;

@end

@implementation XNCareConfirmViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [SVProgressHUD show];
    __weak typeof(self)weakSelf = self;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    XNCareConfirmBillInfoRequest *infoRequest = [[XNCareConfirmBillInfoRequest alloc] initWithOrderId:self.orderId];
    [infoRequest startWithCompletionBlockWithSuccess:^(__kindof XNCareConfirmBillInfoRequest * _Nonnull request) {
        weakSelf.infoModel = request.infoModel;
        dispatch_group_leave(group);
    } failure:^(__kindof XNCareConfirmBillInfoRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_enter(group);
    XNCareConfirmApproveRequest *approveRequest = [[XNCareConfirmApproveRequest alloc] initWithOrderId:self.orderId];
    [approveRequest startWithCompletionBlockWithSuccess:^(__kindof XNCareConfirmApproveRequest * _Nonnull request) {
        weakSelf.approveArray = request.approveFlowArray;
        dispatch_group_leave(group);
    } failure:^(__kindof XNCareConfirmApproveRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_notify(group, queue, ^{
        [weakSelf creatTableData];
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            [weakSelf.tableView reloadData];
        });

    });
    
}





- (void)creatTableData
{
    
    XNTableShowDataModel *infoModel = [[XNTableShowDataModel alloc] init];
    infoModel.showType = XNShowType_Normal;
    infoModel.title = @"单据信息";
    XNShowItemModel *itemModel = [[XNShowItemModel alloc] init];
    itemModel.showOverImage = self.infoModel.carePushAppForm.budgetItemObject.remainApplyMoney < 0;
    NSArray *showItemArray = [NSArray arrayWithObjects:
                              [NSString stringWithFormat:@"单据编号： %@",self.infoModel.ID],
                              [NSString stringWithFormat:@"制单日期： %@",self.infoModel.createTimeStr],
                              [NSString stringWithFormat:@"制单人： %@",self.infoModel.createUserName],
                              [NSString stringWithFormat:@"制单部门： %@",self.infoModel.createUserObject.departmentName],
                              [NSString stringWithFormat:@"职务： %@",self.infoModel.createUserObject.positionName],
                              nil];
    itemModel.showItemArray = showItemArray;
    [infoModel.showDataArray addObject:itemModel];
    [self.showArray addObject:infoModel];
    
    XNTableShowDataModel *submitApplyModel = [[XNTableShowDataModel alloc] init];
    submitApplyModel.showType = XNShowType_Normal;
    submitApplyModel.title = @"申请信息";
    XNShowItemModel *itemModel2 = [[XNShowItemModel alloc] init];
    NSArray *showItemArray2 = [NSArray arrayWithObjects:
                               [NSString stringWithFormat:@"申请单: %@",self.infoModel.carePushAppFormId],
                               [NSString stringWithFormat:@"保修日期: %@",self.infoModel.carePushAppForm.careDateStr],
                               [NSString stringWithFormat:@"申请部门： %@",self.infoModel.carePushAppForm.departmentName],
                               [NSString stringWithFormat:@"申请人: %@",self.infoModel.carePushAppForm.applicantUserName],
                               [NSString stringWithFormat:@"设备名称： %@",self.infoModel.carePushAppForm.name],
                               [NSString stringWithFormat:@"维修保养类型： %@",self.infoModel.carePushAppForm.typeName],
                               [NSString stringWithFormat:@"设备编号： %@",self.infoModel.carePushAppForm.careNo],
                               [NSString stringWithFormat:@"保修类型： %@",self.infoModel.typeName],
                               [NSString stringWithFormat:@"上次里程数： %.2f",self.infoModel.preMileage],
                               [NSString stringWithFormat:@"本次里程数： %.2f",self.infoModel.nowMileage],
                               [NSString stringWithFormat:@"故障描述/保养说明： %@",self.infoModel.carePushAppForm.careContent],
                               [NSString stringWithFormat:@"维修说明： %@",self.infoModel.explanation],
                               [NSString stringWithFormat:@"是否暂支： %@",self.infoModel.isTemp ? @"是":@"否"],
                               [NSString stringWithFormat:@"是否无费用： %@",self.infoModel.isFree ? @"是":@"否"],
                               nil];
    itemModel2.showItemArray = showItemArray2;
    [submitApplyModel.showDataArray addObject:itemModel2];
    [self.showArray addObject:submitApplyModel];
    
    if (self.infoModel.isTemp) {
        XNTableShowDataModel *advanceModel = [[XNTableShowDataModel alloc] init];
        advanceModel.showType = XNShowType_Normal;
        advanceModel.title = @"暂支单";
        XNShowItemModel *advanceItemModel = [[XNShowItemModel alloc] init];
        NSArray *advanceShowItemArray = [NSArray arrayWithObjects:
                                   [NSString stringWithFormat:@"暂支事由： %@",self.infoModel.tempReason],
                                   [NSString stringWithFormat:@"暂支金额： %.3f",self.infoModel.tempCost],
                                   [NSString stringWithFormat:@"预计归还日期： %@",self.infoModel.returnDateStr],
                                   nil];
        advanceItemModel.showItemArray = advanceShowItemArray;
        [advanceModel.showDataArray addObject:advanceItemModel];
        [self.showArray addObject:advanceModel];
    }
    
    XNTableShowDataModel *showModel4 = [[XNTableShowDataModel alloc] init];
    showModel4.showType = XNShowType_Normal;
    showModel4.title = @"维修清单确认";
    [self.infoModel.personList enumerateObjectsUsingBlock:^(XNPersonModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *temp = [[XNShowItemModel alloc] init];
        NSArray *showItemArray4 = [NSArray arrayWithObjects:
                                   [NSString stringWithFormat:@"人工项目： %@",obj.actionName],
                                   [NSString stringWithFormat:@"人工工时： %.2f",obj.workTime],
                                   [NSString stringWithFormat:@"人工单价： %.3f",obj.cost],
                                   [NSString stringWithFormat:@"人工金额： %.3f",obj.totalCost],
                                   nil];
        temp.showItemArray = showItemArray4;
        [showModel4.showDataArray addObject:temp];
    }];
    [self.showArray addObject:showModel4];
    
    
    
    XNTableShowDataModel *showModel3 = [[XNTableShowDataModel alloc] init];
    showModel3.showType = XNShowType_Normal;
    showModel3.title = @"使用部门验收";
    XNShowItemModel *itemModel3 = [[XNShowItemModel alloc] init];
    NSArray *showItemArray3 = [NSArray arrayWithObjects:
                               [NSString stringWithFormat:@"使用部门: %@",self.infoModel.checkDepartmentName],
                               [NSString stringWithFormat:@"验收确认人： %@",self.infoModel.checkUserObject.userName],
                               nil];
    itemModel3.showItemArray = showItemArray3;
    [showModel3.showDataArray addObject:itemModel3];
    [self.showArray addObject:showModel3];
    
    XNTableShowDataModel *remarkModel = [[XNTableShowDataModel alloc] init];
    remarkModel.showType = XNShowType_Normal;
    remarkModel.title = @"备注";
    if (self.infoModel.remark.length > 0) {
        XNShowItemModel *temp = [[XNShowItemModel alloc] init];
        temp.showItemArray = [NSArray arrayWithObject:self.infoModel.remark];
        [remarkModel.showDataArray addObject:temp];
    }
    [self.showArray addObject:remarkModel];
    
    
    XNTableShowDataModel *showModel7 = [[XNTableShowDataModel alloc] init];
    showModel7.showType = XNShowType_Normal;
    showModel7.title = @"其它维修费";
    XNShowItemModel *itemModel7 = [[XNShowItemModel alloc] init];
    NSArray *showItemArray7 = [NSArray arrayWithObjects:
                               [NSString stringWithFormat:@"%@预算总金额:%.3f",self.infoModel.carePushAppForm.budgetItemObject.budgetInfo,self.infoModel.carePushAppForm.budgetItemObject.allMoney],
                               [NSString stringWithFormat:@"已报销： %.3f",self.infoModel.carePushAppForm.budgetItemObject.reimbursedMoney],
                               [NSString stringWithFormat:@"报销中： %.3f",self.infoModel.carePushAppForm.budgetItemObject.reimbursingMoney],
                               [NSString stringWithFormat:@"剩余可报销： %.3f",self.infoModel.carePushAppForm.budgetItemObject.remainReimburseMoney],
                               [NSString stringWithFormat:@"已申请未报销： %.3f",self.infoModel.carePushAppForm.budgetItemObject.applyedMoney],
                               [NSString stringWithFormat:@"在申请未报销： %.3f",self.infoModel.carePushAppForm.budgetItemObject.applyingMoney],
                               [NSString stringWithFormat:@"剩余可申请： %.3f",self.infoModel.carePushAppForm.budgetItemObject.remainApplyMoney],
                               nil];
    itemModel7.showItemArray = showItemArray7;
    [showModel7.showDataArray addObject:itemModel7];
    [self.showArray addObject:showModel7];
    
    
    XNTableShowDataModel *approveModel = [[XNTableShowDataModel alloc] init];
    approveModel.showType = XNShowType_Approve;
    approveModel.title = @"审批流程";
    [self.approveArray enumerateObjectsUsingBlock:^(XNBillApproveModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *temp = [[XNShowItemModel alloc] init];
        temp.showItemArray = [NSArray arrayWithObject:obj];
        [approveModel.showDataArray addObject:temp];
    }];
    [self.showArray addObject:approveModel];
    
}

@end
